<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务，帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types=1);

namespace app\api\controller;

use app\common\library\helper;
use app\common\model\OrderRefundAddress as OrderRefundAddressModel;
use app\common\model\Express as ExpressModel;
use app\common\model\OrderRefund as OrderRefundModel;

/**
 * 第三方数据回调
 * Class Callback
 * @package app\api\controller
 */
class Callback extends Controller
{
    /**
     * 上门取件回调接口
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function csentOrder()
    {
        $data = $this->request->param();
        if(empty($data)){
            return $this->renderError('参数不能为空');
        }
        log_record($data);
        $param  = helper::jsonDecode(str_replace('&quot;','"',$data['param']));
        $log_file = runtime_root_path() . 'api/log/kuaidi100_' . date('y-m-d') . '.log';
        file_put_contents($log_file, print_r($param, true) . "\n", FILE_APPEND);

        //验证签名
        $sign = strtoupper(MD5 (str_replace('&quot;','"',$data['param']) .'bangnuo'));
        if($sign != $data['sign']){
            $callback = ['result'=>false,'returnCode'=>500,'message'=>'签名不一致，回调失败'];
            // 返回状态
            return json($callback);
        }

        if($param['status'] == 200 && !empty($param['data'])){
            $task_order_id = $param['data']['orderId'].','.$param['data']['taskId'];
            $address =  new OrderRefundAddressModel();
             $update_data['callback_result'] = helper::jsonEncode($param['data']);
            //更新订单信息
            $address->where(['task_order_id' => $task_order_id])->update($update_data);
            //更新物流单号
            if($param['kuaidinum'] && $param['kuaidicom']){
                $expressModel = new ExpressModel();
                $express_list = $expressModel->getAll();
                $express_list = array_column($express_list->toArray(),'express_id','kuaidi100_code');
                if(isset($express_list[$param['kuaidicom']])){
                    $OrderRefundModel = new OrderRefundModel();
                    $order_info = [
                        'is_user_send' => 1,
                        'send_time' => time(),
                        'express_id' => (int)$express_list[$param['kuaidicom']],
                        'express_no' => $param['kuaidinum'],
                    ];
                    $info = $address->where(['task_order_id' => $task_order_id])->find()->toArray();
                    if(isset($info['order_refund_id'])){//更新退货物流单号
                        $OrderRefundModel->where(['order_refund_id' => $info['order_refund_id']])->update($order_info);
                    }
                }
            }
            $callback = ['result'=>true,'returnCode'=>200,'message'=>'提交成功'];
            return json($callback);
        }
        $callback = ['result'=>false,'returnCode'=>500,'message'=>'数据回调失败'];
        // 返回状态
        return json($callback);
    }
    /**
     * 上门取件回调接口
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function poll()
    {
        $data = $this->request->param();
        if(empty($data)){
            return $this->renderError('参数不能为空');
        }
        $log_file = runtime_root_path() . 'api/log/kuaidi100_poll_' . date('y-m-d') . '.log';
        file_put_contents($log_file, print_r($data, true) . "\n", FILE_APPEND);
    }
}
